import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:t2app/src/common/AppData.dart';

import '../Config.dart';

class Tool{

  /*
  检查用户信息
   */
  static bool checkUserInfo(){
    return AppData.user==null?false:true;
  }
  /*
    强制竖屏
     */
  static void  setForcedVerticalscreen (){
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown
    ]);
  }

  /*
    强制横屏
     */
  static void  setForcedHorizontalScreen (){
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeLeft,
      DeviceOrientation.landscapeRight
    ]);
  }

  /**
   *  错误提示框
   */
  static void  EasyLoading_Error(String msg, {duration=2}){
    EasyLoading.showError(msg , duration: new Duration(seconds: duration));
  }
  static void  EasyLoading_Success(String msg,{duration=1}){
    EasyLoading.showSuccess(msg , duration: new Duration(seconds: duration));
  }

  static void  EasyLoading_Info(String msg,{duration=1}){
    EasyLoading.showInfo(msg , duration: new Duration(seconds: duration));
  }
  static void  EasyLoading_Toast(String msg,{duration=1}){
    EasyLoading.showToast(msg , duration: new Duration(seconds: duration));
  }
  /*
  信息提示框
   */
  static void showTipsDialog(BuildContext context, String msg) {
    showDialog(
        context: context,
        builder: (BuildContext context) => Center(
          child: Material(
            color: Colors.transparent,
            child: Container(
              width: MediaQuery.of(context).size.width * 0.8,
              padding: EdgeInsets.fromLTRB(20, 20, 20, 20),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(10)),
                boxShadow: [
                  BoxShadow(
                    color: Colors.grey.withOpacity(0.5),
                    spreadRadius: 2,
                    blurRadius: 5,
                    offset: Offset(0, 3),
                  ),
                ],
              ),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    '提示',
                    style: TextStyle(
                      fontSize: 22,
                      fontWeight: FontWeight.bold,
                      color: Color(0XFF333333),
                      decoration: TextDecoration.none,
                    ),
                  ),
                  Divider(
                    color: Colors.grey.withOpacity(0.5),
                    thickness: 1,
                    height: 30,
                  ),
                  Text(
                    msg,
                    style: TextStyle(
                      fontSize: 18,
                      fontWeight: FontWeight.normal,
                      color: Colors.grey,
                      decoration: TextDecoration.none,
                    ),
                  ),
                  SizedBox(height: 20),
                  Align(
                    alignment: Alignment.centerRight,
                    child: GestureDetector(
                      child: Text(
                        '关闭 ',
                        style: TextStyle(
                          fontSize: 20,
                          fontWeight: FontWeight.w500,
                          color: Config.THEME.primaryColorDark,
                          decoration: TextDecoration.none,
                        ),
                      ),
                      onTap: () {
                        Navigator.of(context).pop();
                      },
                    ),
                  ),
                ],
              ),
            ),
          ),
        ));
  }

  static void showToast(String msg) {
    Fluttertoast.showToast(
        msg: msg,
        toastLength: Toast.LENGTH_LONG,
        gravity: ToastGravity.CENTER,  // 消息框弹出的位置
        timeInSecForIosWeb: 1,  // 消息框持续的时间（目前的版本只有ios有效）
        fontSize: 16.0
    );
  }
}